package cn.scw.project.goods.service.impl;

import cn.scw.common.constant.DictTypeConsts;
import cn.scw.common.constant.GComConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.RegexUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.common.utils.sql.JpaUtil;
import cn.scw.project.goods.domain.GAfterSale;
import cn.scw.project.goods.domain.GCommodityOrder;
import cn.scw.project.goods.domain.netework.bean.GAfterSaleAddReq;
import cn.scw.project.goods.domain.netework.bean.GAfterSaleListReq;
import cn.scw.project.goods.domain.vo.GAfterSaleVo;
import cn.scw.project.goods.mapper.GAfterSaleMapper;
import cn.scw.project.goods.service.IGAfterSaleService;
import cn.scw.project.goods.service.IGCommodityOrderService;
import cn.scw.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 售后Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GAfterSaleServiceImpl implements IGAfterSaleService {
    @Autowired
    private GAfterSaleMapper gAfterSaleMapper;
    @Autowired
    private IGCommodityOrderService igCommodityOrderService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询售后
     *
     * @param id 售后ID
     * @return 售后
     */
    @Override
    public GAfterSale selectGAfterSaleById(Long id) {
        return gAfterSaleMapper.selectGAfterSaleById(id);
    }

    @Override
    public GAfterSaleVo selectGAfterSaleVoById(Long id) {
        return gAfterSaleMapper.selectGAfterSaleVoById(id);
    }

    /**
     * 查询售后列表
     *
     * @param req 售后
     * @return 售后
     */
    @Override
    public List<GAfterSaleVo> selectGAfterSaleList(GAfterSaleListReq req) {
        req.setLike(JpaUtil.getNullOrLikeStr(req.getLike()));
        req.setOrderNumber(JpaUtil.getNullOrLikeStr(req.getOrderNumber()));
        return gAfterSaleMapper.selectGAfterSaleList(req);
    }

    /**
     * 新增售后
     *
     * @param req 售后
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertGAfterSale(GAfterSaleAddReq req) {
        GCommodityOrder order = igCommodityOrderService.selectGCommodityOrderById(req.getOrderId());
        if (order == null)
            throw new CustomException("订单不存在");
        if (!sysDictDataService.existsDictLabel(DictTypeConsts.G_AFTER_SALE_TYPE, req.getType()))
            throw new CustomException("请选择售后方式");
        if (!RegexUtils.isPhone(req.getPhone()))
            throw new CustomException("请填写正确的手机号码");
        if (StringUtils.isBlank(req.getExplain()))
            throw new CustomException("请填写退款说明");

        order.setStatusCode(GComConsts.ORDER_STATUS_AFTER_SALE);
        int i = igCommodityOrderService.updateGCommodityOrder(order);
        if (i < 1)
            throw new CustomException("申请售后失败");
        GAfterSale gAfterSale = new GAfterSale();
        ToolUtils.copyPropertiesIgnoreNull(req, gAfterSale);
        return gAfterSaleMapper.insertGAfterSale(gAfterSale);
    }

    /**
     * 修改售后
     *
     * @param req 售后
     * @return 结果
     */
    @Override
    public int updateGAfterSale(GAfterSaleAddReq req) {
        GAfterSale gAfterSale = this.selectGAfterSaleById(req.getId());
        if (gAfterSale == null)
            throw new CustomException("售后不存在");
        if (req.getIsAgree() == null)
            throw new CustomException("请选择同意或拒绝");
        if (!req.getIsAgree() && StringUtils.isBlank(req.getNote()))
            throw new CustomException("请填写拒绝售后理由");

        gAfterSale.setIsAgree(req.getIsAgree());
        gAfterSale.setNote(req.getNote());
        return gAfterSaleMapper.updateGAfterSale(gAfterSale);
    }

    /**
     * 批量删除售后
     *
     * @param ids 需要删除的售后ID
     * @return 结果
     */
    @Override
    public int deleteGAfterSaleByIds(Long[] ids) {
        return gAfterSaleMapper.deleteGAfterSaleByIds(ids);
    }

    /**
     * 删除售后信息
     *
     * @param id 售后ID
     * @return 结果
     */
    @Override
    public int deleteGAfterSaleById(Long id) {
        return gAfterSaleMapper.deleteGAfterSaleById(id);
    }
}
